<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      /* 
      背包容量为n
      物品为1-n   因为可能n=1  你物品也是1
      weight[i]=Math.sqrt(物品)
      */
      var numSquares = function (n) {
        let bagSize = n
        let dp = new Array(bagSize + 1).fill(Infinity)
        dp[0] = 0
        for (let i = 1; i <= n; i++) {
          let weight = Math.pow(i, 2)
          for (let j = weight; j <= bagSize; j++) {
            dp[j] = Math.min(dp[j], dp[j - weight] + 1)
          }
        }
        return dp[bagSize] == Infinity ? -1 : dp[bagSize]
      }
    </script>
  </body>
</html>
